import { Component, OnInit, Input, ViewChild, Output, EventEmitter } from '@angular/core';
import { NzSelectComponent } from 'ng-zorro-antd'
import { SupDic } from 'lbf';

@Component({
  // tslint:disable-next-line:component-selector
  selector: 'lb-ka03',
  templateUrl: './ka03.html',
})
export class LbKA03Component implements OnInit {
 

  ka03: Array<{ AKE042: string, AKE046: string, AKE023: string }> = [];
  listOfOption: Array<{ AKE042: string, AKE046: string, AKE023: string }> = [];
  selectedValue = '';
  

  @Input() set value(val: any) {
    
    if(!val){
      this.selectedValue=null;
    }else{
      this.listOfOption = this.ka03.filter((item) => item.AKE042 === val);
      this.selectedValue = val;
    }

  }

  get value(): any {
    return this.selectedValue;
  }

 

  @Output() changed =new EventEmitter<string>();

  @ViewChild('select',{static:false}) select: NzSelectComponent;


  reset(value) {
    this.selectedValue = value;

    if(value){
      this.listOfOption = this.ka03.filter((item) => item.AKE042 === value);
    }

  }

  setvalue(value) {
    if(!value)
      this.selectedValue=null;

    this.selectedValue = value;
  }


  searchInput(value) {

    if (!value || value === "") {
      // this.listOfOption = [];
      this.listOfOption = this.ka03.filter((item) => item.AKE042 === value);
      return;
    }

    let idx = 0;

    this.listOfOption = this.ka03.filter(ka03 => {

      if (idx > 100) return;

      if ((ka03.AKE023 + "-" + ka03.AKE042 + "-" + ka03.AKE046).toUpperCase().includes(value.toUpperCase())) {

        idx++;
        return true;
      }

      return false;

    });

  }



  

  // private searchTerms = new Subject<string>(); // 定义这个对象

  search(value: string): void {

    // this.searchTerms.next(value);

    this.searchInput(value);

  }

  change(value:string):void{
    if(this.changed)
      this.changed.emit(value);

  }

  constructor(private supdic: SupDic) {

  }

  ngOnInit(): void {

    this.ka03 = (this.supdic.ka010203.KA03 as Array<{ AKE042: string, AKE046: string, AKE023: string }>).sort((a, b) => {

      return a.AKE046.length - b.AKE046.length;
    });

  }

}